package org.promote.hotspot.dashboard.tool;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import lombok.extern.java.Log;

/**
 * @author enping.jep
 * @date 2024/02/21 16:29
 **/
@Log
public class FlushUtil {

    public static void flush(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf) {
        if (channelHandlerContext.channel().isWritable()) {
            channelHandlerContext.channel().writeAndFlush(byteBuf).addListener(future -> {
                if (!future.isSuccess()) {
                    log.warning("flush error " + future.cause().getMessage());
                }
            });
        } else {
            try {
                //同步发送
                channelHandlerContext.channel().writeAndFlush(byteBuf).sync();
            } catch (InterruptedException e) {
                log.warning("flush error " + e.getMessage());
            }
        }
    }
}
